//## 과목 콤보 Refresh ####################
function fnSetAccGroup()
{
	var valUrl = "/action/article/listAccGroup.asp";
	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		//data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			$("#accGroupCombo").html("<option value=\"\">--과목--</option>");

			$(xml).find("accGROUP").each(function(idx){
				$("#accGroupCombo").append("<option value=\""+$(this).find("accGroup").text()+"\">"+$(this).find("showAccGroup").text()+"</option>");
			});
			$("#accGroupCombo").append("<option value=\"new\">[+ +]신규추가</option>");
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 과목 정보 세팅하기
function fnPutAccGroup(accGroup)
{
	if (accGroup == "new" || accGroup == "") $("#accGroup").val("");
	else $("#accGroup").val(accGroup);
}

//## 계정정보 저장 ####################
function fnSave()
{
	if (!$("#accGroup").val())
	{
		alert("과목을 바르게 입력해 주세요!");
		$("#accGroup").focus();
		return;
	}
	if (!$("#accountNm").val())
	{
		alert("계정을 바르게 해주세요!");
		$("#accountNm").focus();
		return;
	}
	if (!$("#part").val())
	{
		alert("TYPE(수입/지출)을 선택해 주세요!");
		$("#part").focus();
		return;
	}
	
	var valUrl = '/action/article/save.asp';
	var param  = "accountNm="+encodeURIComponent($("#accountNm").val());
		param += "&accGroup="+encodeURIComponent($("#accGroup").val());
		param += "&part="+$("#part").val();
		param += "&accountCd="+$("#accountCd").val();
		param += "&flag="+$("#flag").val();

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 계정정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").show();
	setNullRow("loadSpan","1");
	var valUrl = "/action/article/list.asp";
	var param = "findUserNm="+encodeURIComponent($("#findUserNm").val());
		param += "&findPart="+encodeURIComponent($("#findPart").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Articles").find("ArticleList").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var accountCd		= $(G_grid01Rslt).find("Articles").find("ArticleList").eq(j).find("accountCd").text();
		var accountNm		= $(G_grid01Rslt).find("Articles").find("ArticleList").eq(j).find("accountNm").text();
		var part			= $(G_grid01Rslt).find("Articles").find("ArticleList").eq(j).find("part").text();
		var accGroup		= $(G_grid01Rslt).find("Articles").find("ArticleList").eq(j).find("accGroup").text();
		var flag			= $(G_grid01Rslt).find("Articles").find("ArticleList").eq(j).find("flag").text();

		ji = j+1;
		
		var showFlag = "";
		if(flag == "Y") showFlag = "사용중";
		else showFlag = "중 지";

		var vPart = "";
		if (part == "IN") 
		{
			vPart = "수입";
			var bgColor = "#FFFFFF";
		}
		else if (part == "OT") 
		{	
			vPart = "지출";
			var bgColor = "#f0faff";
		}
		
		if (flag == "N") var bgColor = "#F2F2F2";

		grid01Array[0] = ji;
		grid01Array[1] = vPart;
		grid01Array[2] = accGroup;
		grid01Array[3] = accountNm;
		grid01Array[4] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,,,", "fnSetArticle('"+accountCd+"')", boldLine);
	}
}


//## 계정정보 상세정보 표시 ####################
function fnSetArticle(accountCd)
{
	var valUrl = "/action/article/view.asp";
	var param = "accountCd="+accountCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);
	fnSetAccGroup();

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();

			$("#accountCd").val($(xml).find("accountCd").text());
			$("#accountNm").val($(xml).find("accountNm").text());
			$("#accGroup").val($(xml).find("accGroup").text());
			$("#accGroupCombo").val($(xml).find("accGroup").text());
			$("#part").val($(xml).find("part").text());
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 계정정보 Reset 기능
function fnReset()
{
	$("#accGroup").val("");
	$("#accountCd").val("");
	$("#accountNm").val("");
	$("#part").val("");
	$("#flag").val("Y");
	$("#accGroupCombo").val("");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findAccountNm").val("");
	$("#findPart").val("");
}